package com.example.common.common.app;

import android.os.SystemClock;
import android.widget.Toast;

import net.qiujuer.genius.kit.handler.Run;
import net.qiujuer.genius.kit.handler.runable.Action;

import java.io.File;

/**
 * Created by HongQian.Wang on 2017/9/19.
 */

public class Application extends android.app.Application {
          private static  Application instance;


    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
    }
    public static Application getInstance(){
        return instance;
    }

    /**
     * 获取缓存头像文件夹地址
     * @return
     */
    public static File getCacheDirFile(){
        return instance.getCacheDir();
    }

    public  static File getPortraitImFile(){
        //在缓存文件夹下创建一个名为portrait的文件夹
        File dir = new File(getCacheDirFile(),"portrait");
        if(!dir.exists()) {
            dir.mkdirs();  //创建所有的对应的文件夹
        }

        File[] files=dir.listFiles();
        if(files!=null && files.length>0) {
             for (File file :files){
                 file.delete();
             }
        }

        //创建一个名称为当时时间戳的文件
        File path=new File(dir, SystemClock.currentThreadTimeMillis()+".jpg");

        return path.getAbsoluteFile();
    }

    /**
     * 获取录音文件的缓存地址
     * @param isTmp  isTmp 是否是缓存文件
     * @return
     */
    public static File getAudioTemFile(boolean isTmp){
        File dir = new File(getCacheDirFile(), "audio");
         
        if(!dir.exists()) {
            dir.mkdirs();
        }
        File[] files = dir.listFiles();
        if(files!=null&& files.length>0) {
             for (File file :files){
                 file.delete();
             }
        }
        File path = new File(getCacheDirFile(), isTmp ? "tmp.mp3" : SystemClock.currentThreadTimeMillis() + ".mp3");
        return path.getAbsoluteFile();

    }

    public static void showToast(final String msg){

        //异步线程谈土司
            Run.onUiAsync(new Action() {
                @Override
                public void call() {
                    Toast.makeText(instance, msg, Toast.LENGTH_SHORT).show();
                }
            });
    }

    public static void showToast( int msgId){
        showToast(instance.getResources().getString(msgId));
    }
}
